#include "service_process_list_item.h"
#include <qlabel.h>
#include <qlayout.h>
#include <qdebug.h>
#include "switch_button.h"

ServiceProcessListItem::ServiceProcessListItem(QWidget *parent)
	: QWidget(parent)
{
	initControls();
	initLayout();
}

ServiceProcessListItem::~ServiceProcessListItem()
{
}

void ServiceProcessListItem::setNumber(const QString& number)
{
	labNumber_->setText(number);
}

void ServiceProcessListItem::setAppName(const QString & appName)
{
	labAppName_->setText(appName);
	labAppName_->setToolTip(appName);
}

void ServiceProcessListItem::setAppPath(const QString & appPath)
{
	labAppPath_->setText(appPath);
	labAppPath_->setToolTip(appPath);
}

void ServiceProcessListItem::setRunningState(const bool & state)
{
	swbtnRunningState_->setSwitch(state);
}

void ServiceProcessListItem::setEnableGuard(const bool & enable)
{
	swbtnEnableGuard_->setSwitch(enable);
}

QString ServiceProcessListItem::getNumber()
{
	return labNumber_->text();
}

QString ServiceProcessListItem::getAppName()
{
	return labAppName_->text();
}

void ServiceProcessListItem::resizeEvent(QResizeEvent * event)
{
	qDebug() << event->size();
}


void ServiceProcessListItem::initControls()
{
	resize(400, 30);

	labNumber_ = new QLabel(this);
	labAppName_ = new QLabel(this);
	labAppPath_ = new QLabel(this);
	swbtnRunningState_ = new SwitchButton(this);
	swbtnEnableGuard_ = new SwitchButton(this);
	labNumber_->setFixedWidth(25);
	labAppName_->setMouseTracking(true);
	labAppPath_->setMouseTracking(true);


	int fixedHeight = 30;
	labNumber_->setFixedSize(50 , fixedHeight);
	labAppName_->setFixedSize(200, fixedHeight);
	labAppPath_->setFixedHeight(fixedHeight);

	int swBtnWidth = 62;
	int swBtnHeight = 28;
	swbtnRunningState_->setFixedSize(swBtnWidth, swBtnHeight);
	swbtnEnableGuard_->setFixedSize(swBtnWidth, swBtnHeight);
	swbtnRunningState_->setEnabled(false);

	connect(swbtnEnableGuard_, &SwitchButton::sigSwitchChanged, this, [this](bool status) {
		emit signGuardianStatusChanged(this->getNumber(), this->getAppName(), status);
	});
}

void ServiceProcessListItem::initLayout()
{
	//QHBoxLayout* hLayoutLabel = new QHBoxLayout();
	//hLayoutLabel->addWidget(labels_[Number], 1);
	//hLayoutLabel->addWidget(labels_[AppName], 4);
	//hLayoutLabel->addWidget(labels_[AppPath], 14);
	//hLayoutLabel->addWidget(labels_[RunningState], 2);
	//hLayoutLabel->addWidget(labels_[EnableGuard], 2);
	//hLayoutLabel->setContentsMargins(QMargins(10, 0, 10, 0));
	//hLayoutLabel->setSpacing(20);

	QHBoxLayout* mainLayout = new QHBoxLayout(this);
	mainLayout->addWidget(labNumber_);
	mainLayout->addWidget(labAppName_);
	mainLayout->addWidget(labAppPath_);
	mainLayout->addWidget(swbtnRunningState_);
	mainLayout->addWidget(swbtnEnableGuard_);
	mainLayout->setContentsMargins(QMargins(10, 0, 10, 0));
	mainLayout->setSpacing(10);
	this->setLayout(mainLayout);
}